<?php

namespace Es\Operate;

use Es\Client;
use Es\Exceptions\CommonExceptions;
use Es\Struct\{ConnectStruct,DocumentStruct};

/**
 * 单个文档操作类
 */
class Document extends Client
{
    /**
     * @var DocumentStruct
     */
    private $document;

    /**
     * @param ConnectStruct $connect
     * @param DocumentStruct $document
     * @throws CommonExceptions
     */
    public function __construct(ConnectStruct $connect, DocumentStruct $document)
    {
        parent::__construct($connect);

        $this->document = $document;
    }

    /**
     * @return array|callable
     * @throws CommonExceptions
     */
    public function add()
    {
        if(empty($this->document->body)){
            throw new  CommonExceptions(CommonExceptions::DOCU_BODY_NOT_EMPTY);
        }
        try {
            $params = [
                'index' => $this->document->index,
                'type'  => $this->document->type,
                'body'  => $this->document->body
            ];
            if(!empty($this->document->id)){
                $params['id'] = $this->document->id;
            }
            $response = self::$client->index($params);
        } catch (\Throwable $e) {
            throw new CommonExceptions(CommonExceptions::ERROR_OPERATE, $e->getMessage());       
        }
        return $response;
    }

    /**
     * @return array|callable
     * @throws CommonExceptions
     */
    public function update()
    {
        if(empty($this->document->id)){
            throw new  CommonExceptions(CommonExceptions::DOCU_ID_NOT_EMPTY);
        }
        if(empty($this->document->body)){
            throw new  CommonExceptions(CommonExceptions::DOCU_BODY_NOT_EMPTY);
        }
        try { 
            $params = [
                'index' => $this->document->index,
                'type'  => $this->document->type,
                'id'    => $this->document->id,
                'body'  => [
                    'doc' => $this->document->body,
                ]
            ];
            $response = self::$client->update($params);
        } catch (\Throwable $e) {
            throw new CommonExceptions(CommonExceptions::ERROR_OPERATE, $e->getMessage());       
        }
        return $response;
    }

    /**
     * @return array|callable
     * @throws CommonExceptions
     */
    public function info()
    {
        if(empty($this->document->id)){
            throw new  CommonExceptions(CommonExceptions::DOCU_ID_NOT_EMPTY);
        }
        try{
            $params = [
                'index' => $this->document->index,
                'type'  => $this->document->type,
                'id'    => $this->document->id,
            ];
            $response = self::$client->get($params);
        }catch(\Throwable $e){
            throw new CommonExceptions(CommonExceptions::ERROR_OPERATE, $e->getMessage());       
        }
        return $response;
        
    }

    /**
     * @return array|callable
     * @throws CommonExceptions
     */
    public function delete()
    {
        if(empty($this->document->id)){
            throw new  CommonExceptions(CommonExceptions::DOCU_ID_NOT_EMPTY);
        }
        try{
            $params = [
                'index' => $this->document->index,
                'type'  => $this->document->type,
                'id'    => $this->document->id,
            ];
            $response = self::$client->delete($params);
        }catch(\Throwable $e){
            throw new CommonExceptions(CommonExceptions::ERROR_OPERATE, $e->getMessage());       
        }
        return $response;   
    }
}
